package study.springboot.mvc.auth.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@Order(1)
public class AuthFilter extends OncePerRequestFilter {

//    @Value("#{'${auth.ignore-list}'.split(',')}")
    @Value("${auth.ignore-list}")
    private List<String> authWhiteLt;
    @Value("#{${auth.ignore-map}}")
//    @Value("${auth.ignore-map}")
    private Map<String, String> authIgnoreMap;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain chain) throws ServletException, IOException {
        log.info("{}", authWhiteLt);
        String uri = request.getRequestURI();
        if (authWhiteLt.contains(uri)) {
            log.info("ignore auth uri: [{}]", uri);
            doFilter(request, response, chain);
            return;
        }
        try {
            //Step-1: auth


            //Step-2: session

            //next filter
            doFilter(request, response, chain);
        } catch (Exception ex) {
            throw ex;
        }
    }
}
